#include "ipc/EidList.h"
#include <string.h>
#include "log.h"

EidList::EidList(const char* path, int id):memoria(path, id, EID_LIST_MAX*sizeof(EID)){

}

void EidList::insertar(long id, const char* uri){
    EID* base = (EID*) memoria.getBase();
    int i=0;

    clog(ROJO, "Inserto %s\n", uri);

    while(base[i].id != id && base[i].id != 0)
	i++;
    
    base[i].id = id;
    strcpy((char*)&(base[i].uri), uri);
    base[i+1].id=0;
}

EID EidList::obtener(long id){
    EID* base = (EID*) memoria.getBase();
    EID vuelta;
    int i=0;

    while(base[i].id != id && base[i].id != 0)
	i++;
    
    if(base[i].id == id){
	memcpy((void*)&vuelta, (void*)&(base[i]), sizeof(EID));
	return vuelta;
    }
    else{
	EID nulo;
	memset((void*)&nulo, 0, sizeof(nulo));
	return nulo;
    }
}

EID EidList::obtener(const char* uri){
    EID* base = (EID*) memoria.getBase();
    EID vuelta;
    int i=0;

    clog(MAGENTA, "Busco %s en latabla\n", uri);
    while(strcmp(base[i].uri,uri) != 0 && base[i].uri[0] != 0){
	clog(MAGENTA, "Comparo con %s\n", base[i].uri);
	i++;
    }
    clog(MAGENTA, "Comparo con %s\n", base[i].uri);
    
    if(base[i].uri[0] != 0){
	memcpy((void*)&vuelta, (void*)&(base[i]), sizeof(EID));
	return vuelta;
    }
    else{
	EID nulo;
	memset((void*)&nulo, 0, sizeof(nulo));
	return nulo;
    }
}

void EidList::reset(){
    memoria.borrar();
}
